﻿using MgSoft.K3Cloud.WebApi.Dto;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace MgSoft.K3Cloud.WebApi
{
    /// <summary>
    /// 金蝶云星空登录信息
    /// </summary>
    public class K3CloudClientInfo
    {
        public K3CloudClientInfo()
        {
        }

        public K3CloudClientInfo(string serverUrl, string dbid, string userName, string password, int lcid)
        {
            ServerUrl = serverUrl;
            Dbid = dbid;
            UserName = userName;
            Password = password;
            Lcid = lcid;
        }

        public K3CloudClientInfo(ApiServerInfo apiServerInfo)
        {
            serverUrl = apiServerInfo.ServerUrl;
            Dbid = apiServerInfo.Dbid;
            UserName = apiServerInfo.UserName;
            Password = apiServerInfo.Password;
            Lcid = apiServerInfo.Lcid;
        }

        [JsonIgnore]
        private string serverUrl { get; set; }
        /// <summary>
        /// 服务器地址
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Include)]
        public string ServerUrl
        {
            get
            {
                var uri = new Uri(serverUrl);
                return $"{uri.Scheme}://{uri.Authority}/K3Cloud/";
            }
            set
            {
                serverUrl = value;
            }
        }
        /// <summary>
        /// 账套Id
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Include)]
        public string Dbid { get; set; }
        /// <summary>
        /// 用户名
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Include)]
        public string UserName { get; set; }
        /// <summary>
        /// 密码
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Include)]
        public string Password { get; set; }
        /// <summary>
        /// 默认中文
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Include)]
        public int Lcid { get; set; } = 2052;
        /// <summary>
        /// 登录成功时间
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Include)]
        public DateTime? LoginSuccessTime { get; set; }
        /// <summary>
        /// 登录后返回的kdservice-sessionid
        /// </summary>        
        [JsonProperty(NullValueHandling = NullValueHandling.Include)]
        public string kdservice_sessionid { get; set; }
        /// <summary>
        /// 登录后返回的ASP.NET_SessionId
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Include)]
        public string ASP_NET_SessionId { get; set; }

        public override bool Equals(object obj)
        {
            var target = obj as K3CloudClientInfo;
            if (target == null) return false;

            if (target == this) return true;

            if (this.ServerUrl != target.ServerUrl) return false;
            if (this.Dbid != target.Dbid) return false;
            if (this.UserName != target.UserName) return false;
            if (this.Password != target.Password) return false;
            if (this.Lcid != target.Lcid) return false;

            return true;
        }

        public override int GetHashCode()
        {
            return ServerUrl.GetHashCode() ^ Dbid.GetHashCode() ^ UserName.GetHashCode() ^ Password.GetHashCode() ^ kdservice_sessionid.GetHashCode() ^ ASP_NET_SessionId.GetHashCode();
        }
    }
}
